package com.memory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.memory.common.R;
import com.memory.dao.SetMealDao;
import com.memory.daomain.Category;
import com.memory.daomain.Dish;
import com.memory.daomain.Setmeal;
import com.memory.daomain.SetmealDish;
import com.memory.dto.SetmealDto;
import com.memory.service.SetMealService;
import com.memory.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 邓哈哈
 * 2023/1/16 14:43
 * Function:
 * Version 1.0
 */
@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealDao, Setmeal> implements SetMealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetMealDao setMealDao;

    /**
     * 添加套餐
     * 添加对应菜品
     *
     * @param setmealDto
     * @return
     */
    @Override
    public Boolean saveSetmealWithDish(SetmealDto setmealDto) {
        //1.添加套餐信息
        boolean save = this.save(setmealDto);

        //2.添加对应菜品信息
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();

        //获取该套餐的id
        Long setmealId = setmealDto.getId();

        //给该套餐的每道菜品添加id信息
        dishList = dishList.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        boolean saveBatch = setmealDishService.saveBatch(dishList);

        return save && saveBatch;
    }

    /**
     * 删除套餐信息
     * 删除对应菜品信息
     *
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteSetmealWithDish(Long ids) {
        //删除套餐信息
        boolean removeById = this.removeById(ids);

        //删除对应菜品信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ids != null, SetmealDish::getSetmealId, ids);
        boolean remove = setmealDishService.remove(lqw);

        return removeById && remove;
    }

    /**
     * 回显数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto getById(Long id) {
        //1.拿取套餐信息
        Setmeal setmeal = setMealDao.selectById(id);
//        SetmealDto byId = this.getById(id);

        //2.拿取对应菜品信息
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //设置查询条件
        lqw.eq(id != null, SetmealDish::getSetmealId, id);
        //执行查询
        List<SetmealDish> dishList = setmealDishService.list(lqw);

        //3.返回订单信息
        SetmealDto setmealDto = new SetmealDto();

        BeanUtils.copyProperties(setmeal, setmealDto);//对象拷贝
        setmealDto.setSetmealDishes(dishList);

        return setmealDto;
    }

    /**
     * 修改套餐信息
     * 修改对应菜品信息
     *
     * @param setmealDto
     * @return
     */
    @Override
    public Boolean updateSetmealWithDish(SetmealDto setmealDto) {
        //修改套餐信息
//        int update = setMealDao.updateById(setmealDto);
        boolean update = this.updateById(setmealDto);


        //修改对应菜品信息(先删除,后添加)
        //1.删除原有菜品
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        //获取套餐id
        Long setmealId = setmealDto.getId();
        //设置删除条件
        lqw.eq(setmealId != null, SetmealDish::getSetmealId, setmealId);
        //执行删除
        setmealDishService.remove(lqw);

        //2.添加新的菜品
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();

        dishList = dishList.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());

        boolean saveBatch = setmealDishService.saveBatch(dishList);

        return update && saveBatch;
    }

    /**
     * 分页查询
     * 条件查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<SetmealDto> getPage(Integer page, Integer pageSize, Setmeal setmeal) {
        //1.封装Page类
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page, pageSize);

        //2.设置查询条件

        //3.执行分页查询
        setMealDao.selectPage(setmealPage, null);

        return setmealDtoPage;
    }

    /**
     * 根据套餐分类获取其下所有套餐
     *
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        //设置查询条件
        //1.查询当前套餐分类下的套餐信息
        lqw.eq(setmeal != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        //2.查询正在启售的套餐(状态为1的套餐)
        lqw.eq(Setmeal::getStatus, 1);

        //执行查询
        List<Setmeal> setmealList = this.list(lqw);

        return setmealList;
    }

}
